package com.xuecheng.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.xuecheng.config.AlipayConfig;
import com.xuecheng.dto.AddOrderDTO;
import com.xuecheng.dto.PayRecordDTO;
import com.xuecheng.dto.PayStatusDTO;
import com.xuecheng.exception.XueChengPlusException;
import com.xuecheng.po.XcPayRecord;
import com.xuecheng.service.OrderService;
import com.xuecheng.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@Api(value = "订单支付接口", tags = "订单支付接口")
@Slf4j
@RestController
public class OrderController {
	
	@Resource
	private OrderService orderService;
	
	@Value("${pay.alipay.APP_ID}")
	private String APP_ID;
	@Value("${pay.alipay.APP_PRIVATE_KEY}")
	private String APP_PRIVATE_KEY;
	
	@Value("${pay.alipay.ALIPAY_PUBLIC_KEY}")
	private String ALIPAY_PUBLIC_KEY;
	
	@NotNull
	private static AlipayTradeWapPayRequest getAlipayTradeWapPayRequest(XcPayRecord payRecord) {
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
		//告诉支付宝支付结果通知的地址
		alipayRequest.setNotifyUrl("http://tjxt-user-t.itheima.net/xuecheng/orders/receivenotify");//在公共参数中设置回跳和通知地址
		//填充业务参数
		alipayRequest.setBizContent("{" +
			"    \"out_trade_no\":\"" + payRecord.getPayNo() + "\"," +//商户网站唯一订单号，本项目指定支付记录的交易号
			"    \"total_amount\":" + payRecord.getTotalPrice() + "," +
			"    \"subject\":\"" + payRecord.getOrderName() + "\"," +
			"    \"product_code\":\"QUICK_WAP_WAY\"" +
			"  }");
		return alipayRequest;
	}
	
	@ApiOperation("生成支付二维码")
	@PostMapping("/generatepaycode")
	@ResponseBody
	public PayRecordDTO generatePayCode(@RequestBody AddOrderDTO addOrderDto) {
		//登录用户
		SecurityUtil.XcUser user = SecurityUtil.getUser();
		//用户id
		String userId = null;
		if (user != null) {
			userId = user.getId();
		}
		
		return orderService.createOrder(userId, addOrderDto);
	}
	
	@ApiOperation("扫码下单接口")
	@GetMapping("/requestpay")
	public void requestpay(String payNo, HttpServletResponse httpResponse) throws IOException, AlipayApiException {
		//校验payNo支付记录交易号是否存在
		XcPayRecord payRecord = orderService.getPayRecordByPayno(payNo);
		if (payRecord == null) {
			XueChengPlusException.cast("支付记录交易号不存在");
		}
		AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, APP_ID, APP_PRIVATE_KEY, AlipayConfig.FORMAT,
			AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);//获得初始化的AlipayClient
		AlipayTradeWapPayRequest alipayRequest = getAlipayTradeWapPayRequest(payRecord);
		String form = client.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
		httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
		httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
		httpResponse.getWriter().flush();
	}
	
	//接收支付宝支付结果通知
	@RequestMapping("/receivenotify")
	public void receivenotify(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException,
		IOException {
		
		//获取支付宝POST过来反馈信息
		Map<String, String> params = new HashMap<String, String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
					: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
			params.put(name, valueStr);
		}
		//验签名
		boolean verify_result = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2");
		
		if (verify_result) {//验证成功
			//商户订单号
			String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
			//支付宝交易号
			String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
			//交易状态
			String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
			//支付宝通过我们的appid
			String appid = new String(request.getParameter("app_id").getBytes("ISO-8859-1"), "UTF-8");
			//总金额
			String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
			
			if (trade_status.equals("TRADE_SUCCESS")) {
				System.out.println("================支付成功==================");
				//更新订单表的状态
				//封装一个
				PayStatusDTO payStatusDto = new PayStatusDTO();
				//支付宝通过我们的appid
				payStatusDto.setApp_id(appid);
				//支付结果
				payStatusDto.setTrade_status(trade_status);
				//商户自己的订单号
				payStatusDto.setOut_trade_no(out_trade_no);
				//总金额
				payStatusDto.setTotal_amount(total_amount);
				//支付宝自己的订单
				payStatusDto.setTrade_no(trade_no);
				
				orderService.saveAliPayStatus(payStatusDto);
				
			}
			
			//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
			response.getWriter().println("success");
			
		} else {//验证失败
			response.getWriter().println("fail");
		}
	}
}

